//index.js
//获取应用实例
const app = getApp()
var appData = app.globalData
Page({
    data: {
        user_id: '',
        phone: '',
        code: '',
        isSend: true, //是否显示发送验证码字
        count_down: '60',//倒计时
    },
    onLoad: function (options) {
        this.data.user_id = options.user_id
    },
    // 手机号码框失去焦点
    blurPhone: function (e) {
        this.data.phone = e.detail.value
    },
    // 获取验证码
    getCode: function (e) {
        let _this = this
        let phone = this.data.phone
        console.log(this.data.phone)
        if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(phone))) {
            wx.showModal({
                title: '提示',
                content: '您输入的手机号码格式有误',
                showCancel: false,
                success: function (res) {
                    if (res.confirm) {
                    }
                }
            })
            return false;
        } 
        // 请求发送验证码接口
        wx.request({
            url: appData.apiUrl + 'user/getcode',
            data: {
                mobile: phone
            }, 
            success: function (res) {
                _this.setData({
                    isSend: false
                })
                _this.countDown()
                wx.showToast({
                    title: res.data.data,
                    icon: 'success',
                    duration: 1000
                })
            }
        })
    },
    // 提交手机
    formSubmitPhone: function (e) {
        let _this = this
        let data = e.detail.value
        let phone = data.phone
        let code = data.code
        let user_id = _this.data.user_id
        if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(phone))) {
            wx.showToast({
                title: '您输入的手机号码格式有误',
                icon: 'none',
                duration: 1000
            })
            return false;
        } 
        // 请求手机绑定
        wx.request({
            url: appData.apiUrl + 'user/bandmobile',
            data: {
                mobile: phone,
                user_id: user_id,
                code: code
            },
            success: function (res) {
                if (res.data.code == 0) {
                    // 绑定成功返回
                    wx.redirectTo({
                        url: '/pages/personal/personal?user_id=' + user_id,
                    })
                } else {
                    wx.showToast({
                        title: res.data.data,
                        icon: 'none',
                        duration: 1000
                    })
                }
            }
        })
    },
    //倒计时
    countDown: function () {
        var _this = this;
        var count_down = _this.data.count_down
        _this.timeIng = setInterval(function () {
            count_down = count_down - 1
            _this.setData({
                count_down: count_down
            })
            if (count_down == '0') {
                clearInterval(_this.timeIng)
                _this.setData({
                    count_down: 60,
                    isSend: true
                });
            }
        }, 1000)
    }
})
